var gulp = require('gulp');
var del = require('del');

var less = require('gulp-less');
var concat = require('gulp-concat');
var cleanCss = require('gulp-clean-css');
var babel = require('gulp-babel');
var uglify = require('gulp-uglify');
var rename = require('gulp-rename');
var imagemin = require('gulp-imagemin');

gulp.task('clean',function () {//清除文件
    del(['./build/**/*'])
});

gulp.task('html',function () {//html处理
   gulp.src('./src/*.html')
       .pipe(gulp.dest('./build'));
});

gulp.task('less',function () {//less处理
    gulp.src('./src/less/*.less')
        .pipe(less())
        .pipe(concat('main.css'))
        .pipe(cleanCss())
        .pipe(rename('./main.min.css'))
        .pipe(gulp.dest('./build/css/'))
});

gulp.task('img',function () {//图片处理
    gulp.src('./src/img/*.*')
        .pipe(imagemin())
        .pipe(gulp.dest('./build/img/'));
});

gulp.task('js',function () {//js处理
    gulp.src('./src/js/*.js')
        .pipe(babel({presets: ['latest']}))
        .pipe(concat('main.js'))
        .pipe(uglify())
        .pipe(rename('./main.min.js'))
        .pipe(gulp.dest('./build/js/'))
});

gulp.task('watch',function () {
    gulp.watch('src/*.html',['html']);
    gulp.watch('src/less/*.less',['less']);
    gulp.watch('src/img/*.*',['img']);
    gulp.watch('src/js/*.js',['js']);
});

gulp.task('build',['clean','html','less','img','js']);

gulp.task('default',['build','watch']);